package com.bys.core;

import com.bys.pojo.MappedStatement;
import com.bys.transaction.Transaction;

import javax.sql.DataSource;
import java.util.Map;

/**
 * SqlSessionFactory
 * 一个数据库对应一个SqlSessionFactory对象
 * 通过SqlSessionFactory 对象可以获取SqlSession 对象 （开启会话）
 * 一个SqlSessionFactory 可以开启多个SqlSession会话
 * @author bys
 * @version 1.0
 * @since 1.0
 */
public class SqlSessionFactory {
    /**
     * 事务管理器
     */
    private Transaction transaction;
    /**
     * 数据源
     */
    private DataSource dataSource;
    /**
     * 存储 mapper.xml的mapper的Map
     * key 是 naamespace+id
     * value 是存储 标签的对象
     */
    private Map<String, MappedStatement> mappedStatements;

    public SqlSessionFactory() {
    }

    public SqlSessionFactory(Transaction transaction, Map<String, MappedStatement> mappedStatements) {
        this.transaction = transaction;
        this.mappedStatements = mappedStatements;
    }
    public SqlSession openSession(){
//        开启连接
        transaction.openConnection();
        return new SqlSession(this);
    }

    public Transaction getTransaction() {
        return transaction;
    }

    public void setTransaction(Transaction transaction) {
        this.transaction = transaction;
    }

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public Map<String, MappedStatement> getMappedStatements() {
        return mappedStatements;
    }

    public void setMappedStatements(Map<String, MappedStatement> mappedStatements) {
        this.mappedStatements = mappedStatements;
    }
}
